﻿namespace Windows32
{
    using System;
    using System.Runtime.InteropServices;

    public class Kernel32
    {
        public const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x100;
        public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x2000;
        public const int FORMAT_MESSAGE_FROM_HMODULE = 0x800;
        public const int FORMAT_MESSAGE_FROM_STRING = 0x400;
        public const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
        public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x200;
        public const int FORMAT_MESSAGE_MAX_WIDTH_MASK = 0xff;

        public static string FormatErrorMessage(int ErrorCode)
        {
            char[] charBuffer = new char[0x800];
            int num = FormatMessage(0x1200, 0, ErrorCode, 0, charBuffer, charBuffer.Length, 0);
            if (num > 1)
            {
                return new string(charBuffer, 0, num - 1);
            }
            return null;
        }

        [DllImport("Kernel32.dll", CharSet=CharSet.Auto)]
        public static extern int FormatMessage(int Flags, int PSource, int MessageID, int LanguageID, char[] CharBuffer, int BufferSize, int Arguments);
        [DllImport("Kernel32.dll", CharSet=CharSet.Auto)]
        public static extern uint GetLastError();
    }
}

